/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-21
*/
package com.rzico.account.service;

import com.rzico.account.entity.Deposit;
import com.rzico.account.model.BankInfoVo;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.account.entity.EnterpriseDeposit;
import com.rzico.basics.entity.BankInfo;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.mapper.BankInfoMapper;
import com.rzico.basics.mapper.EnterpriseMapper;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.account.mapper.EnterpriseDepositMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 名厨档案业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class EnterpriseDepositService extends BaseServiceImpl<EnterpriseDeposit, String> {

    @Autowired
    private EnterpriseDepositMapper enterpriseDepositMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private BankInfoMapper bankInfoMapper;

    @Override
    public BaseMapper<EnterpriseDeposit, String> getMapper() {
        return enterpriseDepositMapper;
    }

    //入账
    @Transactional(rollbackFor = Exception.class)
    public void addBalance(Long enterpriseId, BigDecimal value, Integer type, String memo,Long orderId) {

        Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(enterpriseId);

        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",enterpriseId);
        updateParams.put("value",value);
        enterpriseMapper.addBalance(updateParams);

        //更新重新读取余额
        enterprise = enterpriseMapper.selectByPrimaryKey(enterpriseId);

        EnterpriseDeposit deposit = new EnterpriseDeposit();
        deposit.setBalance(enterprise.getBalance());
        deposit.setDebit(BigDecimal.ZERO);
        deposit.setCredit(value);
        deposit.setCreateDate(new Date());
        deposit.setModifyDate(new Date());
        deposit.setDeleted(false);
        deposit.setEnterpriseId(enterpriseId);
        deposit.setMemo(memo);
        deposit.setStatus(2);
        deposit.setType(type);
        deposit.setDeleted(false);
        deposit.setOrderId(orderId);
        super.insertUseGeneratedKeys(deposit);

    }

    //出账
    @Transactional(rollbackFor = Exception.class)
    public void decBalance(Long enterpriseId,BigDecimal value,Integer type,String memo,Long orderId) {

        Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(enterpriseId);

        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id",enterpriseId);
        updateParams.put("value",value);
        enterpriseMapper.decBalance(updateParams);

        //更新重新读取余额
        enterprise = enterpriseMapper.selectByPrimaryKey(enterpriseId);

        if (enterprise.getBalance().compareTo(BigDecimal.ZERO)<0) {
            throw new CustomException("账户余额不足");
        }


        BankInfoVo vo = new BankInfoVo();
        vo.setBankId(enterprise.getBankId());
        vo.setBankType(enterprise.getBankType());
        vo.setBankName(enterprise.getBankName());
        vo.setBankNum(enterprise.getBankNum());
        BankInfo bankInfo = bankInfoMapper.selectByPrimaryKey(enterprise.getBankId());
        vo.setBank(bankInfo.getName());

        EnterpriseDeposit deposit = new EnterpriseDeposit();
        deposit.setBalance(enterprise.getBalance());
        deposit.setDebit(value);
        deposit.setCredit(BigDecimal.ZERO);
        deposit.setCreateDate(new Date());
        deposit.setModifyDate(new Date());
        deposit.setDeleted(false);
        deposit.setEnterpriseId(enterprise.getId());
        deposit.setMemo(memo);
        deposit.setType(type);
        deposit.setStatus(0);
        deposit.setDeleted(false);
        deposit.setOrderId(orderId);
        deposit.setBankInfoVo(vo);
        super.insertUseGeneratedKeys(deposit);

    }

}
